<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目复盘工具：A3 笔记法</title>
    <script src="https://cdn.tailwindcss.com"></script>
    
    <!-- 引入外部JS库，移到 body 底部 -->
    
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;700&family=Source+Han+Sans+CN:wght@300;400;700&display=swap" rel="stylesheet">
    
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6', // 蓝色 (导出文档)
                        secondary: '#10B981', // 绿色
                        danger: '#EF4444', // 红色 (导出PDF)
                        'light-blue-bg': '#EFF6FF', // 浅蓝背景
                        'dark-blue-text': '#1E40AF', // 深蓝文字
                        'icon-blue': '#60A5FA',
                        'icon-green': '#34D399',
                        'icon-purple': '#A78BFA',
                    }
                }
            }
        }
    </script>
    <style>
        /* 关键修改：复用 麦肯锡笔记法 的样式 */
        body {
            font-family: 'Source Han Sans CN', sans-serif;
            font-weight: 300;
            background-color: #f3f4f6; /* bg-gray-100 */
        }
        h1, h2, h3, blockquote {
            font-family: 'Noto Serif SC', serif;
        }

        /* 主内容容器，白色卡片风格 */
        .tool-container {
            width: 1000px; /* A3 垂直布局需要标准宽度 */
            max-width: 90vw;
            background: #ffffff;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.08);
            margin: 40px auto;
            padding: 40px;
        }
        
        /* 导出按钮样式 */
        .export-btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            color: white;
            border: none;
            padding: 12px 28px;
            font-size: 1.0em;
            font-weight: 700;
            border-radius: 8px; /* 圆角矩形 */
            cursor: pointer;
            text-align: center;
            transition: opacity 0.3s, transform 0.2s;
            font-family: 'Source Han Sans CN', sans-serif;
            text-decoration: none;
        }
        .btn-pdf {
            background-color: #EF4444; /* 红色 */
        }
        .btn-pdf:hover { opacity: 0.85; transform: translateY(-1px); }
        
        .btn-doc {
            background-color: #3B82F6; /* 蓝色 */
            margin-left: 16px;
        }
        .btn-doc:hover { opacity: 0.85; transform: translateY(-1px); }

        .export-btn.loading { /* 统一 loading 样式 */
            background-color: #9ca3af;
            cursor: wait;
        }

        /* 笔记法标题图标 */
        .section-title-icon {
            width: 32px;
            height: 32px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #EFF6FF; /* light-blue-bg */
            color: #3B82F6; /* primary */
            flex-shrink: 0;
            margin-right: 12px;
        }

        /* A3 模块标题 (复用 麦肯锡 的 column-header) */
        .column-header {
            display: flex;
            align-items: center;
            font-size: 1.2em;
            font-weight: 700;
            font-family: 'Noto Serif SC', serif;
            margin-bottom: 16px;
            margin-top: 24px; /* 增加模块间距 */
        }
        
        /* 编辑框样式 */
        .editable-box {
            width: 100%;
            min-height: 150px;
            padding: 12px;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            background-color: #f9f9f9;
            font-size: 14px;
            line-height: 1.7;
            cursor: text;
        }
        .editable-box:focus {
            outline: 2px solid var(--theme-primary);
            background-color: #fff;
            border-color: var(--theme-primary);
        }
        .editable-box:empty:before {
            content: "点击填写..."; /* 恢复占位符 */
            color: #aaa;
            font-style: italic;
        }

        /* 打印时隐藏的元素 */
        @media print {
            .no-print { display: none; }
        }
        
    </style>
</head>
<body class="bg-gray-100">

    <!-- 关键修改：使用 .tool-container 替换 .magazine-page -->
    <div class="tool-container" id="a3-export-area">

        <!-- 页面头部 -->
        <header class="mb-8 no-print"> 
            <h1 class="text-3xl font-bold text-gray-800">丰田A3笔记法</h1>
            <p class="text-lg text-gray-500 mt-2">SOP系列：项目复盘工具 (Toyota A3)</p>
        </header>

        <!-- 方法简介 -->
        <div class="bg-light-blue-bg border-l-4 border-primary p-6 rounded-lg my-6 no-print">
            <h3 class="font-bold text-lg text-dark-blue-text">方法简介</h3>
            <p class="text-gray-700 mt-2">
                A3报告是顶尖顾问用于解决复杂问题的“一张纸”思考法。它强迫您将“定义问题”和“解决问题”浓缩在一页纸上，是最高效的复盘工具。
            </p>
        </div>

        <!-- 导出按钮区域 -->
        <div class="my-8 text-left no-print">
            <button class="export-btn btn-pdf" id="pdf-button">
                <span>导出为PDF</span>
            </button>
            <a href="#" class="export-btn btn-doc" id="doc-button">
                <span>导出为文档</span>
            </a>
        </div>

        <!-- 在线填写区域 -->
        <section id="a3-content" class="border-t border-gray-200 pt-8">
            
            <!-- 大标题 -->
            <div class="flex items-center mb-6">
                <div class="section-title-icon" id="title-icon-placeholder">
                    <!-- JS会在此处插入图标 -->
                </div>
                <h2 id="a3-title" class="text-2xl font-bold text-gray-800" contenteditable="true">[请填写项目复盘主题]</h2>
            </div>
            
            <!-- 垂直布局 -->
            <div style="display: block;">
                
                <!-- 定义问题 -->
                <div>
                    <div class="column-header">
                        <span id="icon-bg" class="mr-2 text-icon-blue"></span> <!-- 图标占位符 -->
                        1. Background (背景)
                    </div>
                    <div id="a3-bg" class="editable-box" contenteditable="true"></div>

                    <div class="column-header">
                        <span id="icon-status" class="mr-2 text-icon-blue"></span> <!-- 图标占位符 -->
                        2. Current Status (现状)
                    </div>
                    <div id="a3-status" class="editable-box" contenteditable="true"></div>

                    <div class="column-header">
                        <span id="icon-obj" class="mr-2 text-icon-blue"></span> <!-- 图标占位符 -->
                        3. Objectives (目标)
                    </div>
                    <div id="a3-obj" class="editable-box" contenteditable="true"></div>
                </div>

                <!-- 解决问题 -->
                <div>
                    <div class="column-header">
                        <span id="icon-analysis" class="mr-2 text-icon-green"></span> <!-- 图标占位符 -->
                        4. Problem Analysis (问题分析)
                    </div>
                    <div id="a3-analysis" class="editable-box" contenteditable="true"></div>

                    <div class="column-header">
                        <span id="icon-solutions" class="mr-2 text-icon-green"></span> <!-- 图标占位符 -->
                        5. Solutions (解决方案)
                    </div>
                    <div id="a3-solutions" class="editable-box" contenteditable="true"></div>
                    
                    <div class="column-header">
                        <span id="icon-plan" class="mr-2 text-icon-purple"></span> <!-- 图标占位符 -->
                        6. Plan (行动计划)
                    </div>
                    <div id="a3-plan" class="editable-box" contenteditable="true"></div>
                </div>
            </div>
        </section>
        
    </div> <!-- .tool-container end -->

    <!-- 移到 body 底部 -->
    <script src="https://unpkg.com/lucide@latest"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>

    <script>
        window.onload = function() {
            try {
                // --- 1. 图标加载 ---
                if (typeof lucide !== 'undefined') {
                    // 按钮图标
                    const pdfBtn = document.getElementById('pdf-button');
                    if (pdfBtn) pdfBtn.innerHTML = '<i data-lucide="file-text" class="inline-block w-5 h-5 mr-2"></i> 导出为PDF';
                    
                    const docBtn = document.getElementById('doc-button');
                    if (docBtn) docBtn.innerHTML = '<i data-lucide="file-text" class="inline-block w-5 h-5 mr-2"></i> 导出为文档';
                    
                    // 内容图标
                    document.getElementById('title-icon-placeholder').innerHTML = '<i data-lucide="clipboard-list" class="w-5 h-5"></i>';
                    document.getElementById('icon-bg').innerHTML = '<i data-lucide="book-open" class="w-5 h-5"></i>';
                    document.getElementById('icon-status').innerHTML = '<i data-lucide="bar-chart-2" class="w-5 h-5"></i>';
                    document.getElementById('icon-obj').innerHTML = '<i data-lucide="target" class="w-5 h-5"></i>';
                    document.getElementById('icon-analysis').innerHTML = '<i data-lucide="search" class="w-5 h-5"></i>';
                    document.getElementById('icon-solutions').innerHTML = '<i data-lucide="lightbulb" class="w-5 h-5"></i>';
                    document.getElementById('icon-plan').innerHTML = '<i data-lucide="check-circle" class="w-5 h-5"></i>';
                    
                    lucide.createIcons();
                } else {
                    console.warn("Lucide library not loaded.");
                }

                // --- 2. PDF 生成功能 ---
                const pdfButton = document.getElementById('pdf-button');
                if (pdfButton) {
                    pdfButton.addEventListener('click', async function() {
                        if (typeof html2canvas === 'undefined' || typeof window.jspdf === 'undefined') {
                            alert("PDF生成库加载失败，请检查网络连接并刷新页面。");
                            return;
                        }
                        const { jsPDF } = window.jspdf;
                        const button = this;
                        const exportArea = document.getElementById('a3-export-area'); // 截取整个卡片
                        const title = document.getElementById('a3-title').textContent.trim() || 'A3-Report';

                        button.classList.add('loading');
                        button.innerHTML = '<span><i data-lucide="loader-2" class="inline-block w-5 h-5 mr-2 animate-spin"></i> 正在生成PDF...</span>';
                        if (typeof lucide !== 'undefined') lucide.createIcons();
                        
                        window.scrollTo(0, 0);

                        try {
                            const canvas = await html2canvas(exportArea, {
                                scale: 2,
                                useCORS: true,
                                onclone: (doc) => {
                                    doc.querySelectorAll('.no-print').forEach(el => el.style.display = 'none');
                                    // 移除占位符
                                    doc.querySelectorAll('.editable-box:empty:before').forEach(el => el.style.display = 'none');
                                }
                            });
                            
                            const imgData = canvas.toDataURL('image/png');
                            const pdfWidth = 595.28; // A4 纵向
                            const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
                            
                            const pdf = new jsPDF({
                                orientation: 'portrait',
                                unit: 'pt',
                                format: [pdfWidth, pdfHeight]
                            });
                            pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
                            pdf.save(`${title}.pdf`);

                        } catch (err) {
                            console.error('生成PDF失败: ', err);
                            alert("生成PDF失败，请重试。");
                        } finally {
                            button.classList.remove('loading');
                            button.innerHTML = '<span><i data-lucide="file-text" class="inline-block w-5 h-5 mr-2"></i> 导出为PDF</span>';
                            if (typeof lucide !== 'undefined') lucide.createIcons();
                        }
                    });
                }

                // --- 3. 导出Word文档功能 ---
                const docButton = document.getElementById('doc-button');
                if (docButton) {
                    let isDocFileReady = false;
                    docButton.addEventListener('click', function(e) {
                        if (isDocFileReady) {
                            isDocFileReady = false;
                            setTimeout(() => {
                                this.classList.remove('loading');
                                this.innerHTML = '<span><i data-lucide="file-text" class="inline-block w-5 h-5 mr-2"></i> 导出为文档</span>';
                                if (typeof lucide !== 'undefined') lucide.createIcons();
                            }, 1000);
                            return;
                        }

                        e.preventDefault(); 
                        this.classList.add('loading');
                        this.innerHTML = '<span><i data-lucide="loader-2" class="inline-block w-5 h-5 mr-2 animate-spin"></i> 正在生成文档...</span>';
                        if (typeof lucide !== 'undefined') lucide.createIcons();

                        setTimeout(() => {
                            try {
                                const title = document.getElementById('a3-title').innerHTML;
                                const bg = document.getElementById('a3-bg').innerHTML;
                                const status = document.getElementById('a3-status').innerHTML;
                                const obj = document.getElementById('a3-obj').innerHTML;
                                const analysis = document.getElementById('a3-analysis').innerHTML;
                                const solutions = document.getElementById('a3-solutions').innerHTML;
                                const plan = document.getElementById('a3-plan').innerHTML;

                                // Word 样式
                                const h1Style = "font-family: 'Noto Serif SC', serif; color: #333; text-align: center; font-size: 1.8em; font-weight: 700; margin-bottom: 24px;";
                                const h2Style = "font-family: 'Noto Serif SC', serif; font-size: 1.25em; font-weight: 700; color: #3B82F6; border-bottom: 2px solid #EFF6FF; padding-bottom: 8px; margin-bottom: 12px;";
                                const boxStyle = "padding: 12px; border: 1px solid #e0e0e0; border-radius: 6px; background-color: #f9f9f9; font-size: 14px; height: 150px; font-family: 'Source Han Sans CN', 'Microsoft YaHei', sans-serif; vertical-align: top;";

                                function createModule(title, content) {
                                    return `
                                    <div style="margin-bottom: 20px;">
                                        <h2 style="${h2Style}">${title}</h2>
                                        <table style="width: 100%; border-collapse: collapse;"><tr><td style="${boxStyle}">${content || '&nbsp;'}</td></tr></table>
                                    </div>
                                    `;
                                }

                                const wordHtmlContent = `
                                    <div style="font-family: 'Source Han Sans CN', 'Microsoft YaHei', sans-serif; margin: 30px; max-width: 900px;">
                                        <h1 style="${h1Style}">${title}</h1>
                                        ${createModule('1. Background (背景)', bg)}
                                        ${createModule('2. Current Status (现状)', status)}
                                        ${createModule('3. Objectives (目标)', obj)}
                                        ${createModule('4. Problem Analysis (问题分析)', analysis)}
                                        ${createModule('5. Solutions (解决方案)', solutions)}
                                        ${createModule('6. Plan (行动计划)', plan)}
                                    </div>
                                `;
                                
                                const fullHtml = `<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>${title.replace(/<[^>]*>?/gm, '')}</title></head><body>${wordHtmlContent}</body></html>`;
                                const dataUri = 'data:application/vnd.ms-word;base64,' + btoa(unescape(encodeURIComponent(fullHtml)));
                                
                                this.href = dataUri;
                                this.download = `${title.replace(/<[^>]*>?/gm, '')}.doc`;
                                
                                this.classList.remove('loading');
                                this.innerHTML = '<span><i data-lucide="check-circle" class="inline-block w-5 h-5 mr-2"></i> 已就绪, 请再次点击下载</span>';
                                if (typeof lucide !== 'undefined') lucide.createIcons();
                                
                                isDocFileReady = true;

                            } catch (err) {
                                console.error("生成Word失败:", err);
                                alert("生成文档失败，请重试。");
                                this.classList.remove('loading');
                                this.innerHTML = '<span><i data-lucide="file-text" class="inline-block w-5 h-5 mr-2"></i> 导出为文档</span>';
                                if (typeof lucide !== 'undefined') lucide.createIcons();
                            }
                        }, 100);
                    });
                }
                
            } catch (e) {
                console.error("A fatal error occurred during initialization:", e);
            }
        }
    </script>
</body>
</html>

